package com.isunland.managesystem.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import com.isunland.managesystem.R;

/**
 * Created by lenovo on 2016/7/7.
 */
public class ChooseMainDialogFragment extends DialogFragment {
  protected static final String EXTRA_MAIN =
      "com.isunland.managesystem.ui.ChooseMainDialogFragment.EXTRA_MAIN";
  private static final int[] titleIds =
      { R.string.actualBeginTime, R.string.actualEndTime, R.string.ortherMain };
  private static final String[] titleStrs = new String[titleIds.length];

  @Override public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = new AlertDialog.Builder(getActivity()).setSingleChoiceItems(getTitleStrs(), 0,
        new DialogInterface.OnClickListener() {
          @Override public void onClick(DialogInterface dialog, int which) {
            Intent i = new Intent();
            i.putExtra(EXTRA_MAIN, getTitleStrs()[which]);
            getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, i);
            dialog.dismiss();
          }
        }).create();
    return dialog;
  }

  private String[] getTitleStrs() {
    for (int i = 0; i < titleIds.length; i++) {
      titleStrs[i] = getActivity().getResources().getString(titleIds[i]);
    }
    return titleStrs;
  }
}
